<?php
/*
	Aurus CMS - free OOP CMS
	Copyright (C) 2010  Nestor Yanchuk

	This program is free software: you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation, either version 2 of the License, or
	(at your option) any later version.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
if (! defined('ROOT')) die();
if (isset($_POST['category'])){
	$arr = array(
	'title' => __('Назва новини'),
	'ss' => __('Короткий опис'),
	);
	
	$c = Aurus::getInstance()->checkPostEmpty($arr);
	
	$err = (bool) count($c);
	
	if (!$err) {
		$ss = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['ss']));
		$fs = Mysql::getInstance()->escape(Parser::getInstance()->parse($_POST['fs']));
		if (!$fs) {
			$fs = $ss;
		}
		$tt = Mysql::getInstance()->escape(htmlspecialchars($_POST['title']));
		Mysql::getInstance()->query("UPDATE `".PREFIX."_news` SET title='{$tt}',sstory='{$ss}',fstory='{$fs}',category='{$_POST['category']}' WHERE id='{$_GET['eid']}'");
		Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(__('Зміни збережено')));
	} else {
		Aurus::getInstance()->addContent(Adminarea::getInstance()->adminInfo(implode("<br />",$c)));
	}
} elseif (isset($_GET['eid'])) {
	$id = intval($_GET['eid']);
	$nws = Mysql::getInstance()->getarray("SELECT * FROM `".PREFIX."_news` WHERE id='$id'");
	array_map('stripslashes',$nws);


	$categories = Factory::get('Module_news')->getArrayOfCategories();
	$select = '<select name="category" style="width:200px">';
	$sa = Factory::get('Module_news')->getArrayOfCategories();
	foreach ($sa as $id => $name){
		if ($id == $nws['category']) {
			$select .= '<option selected="selected" value="'.$id.'">'.$name.'</option>';
		} else {
			$select .= '<option value="'.$id.'">'.$name.'</option>';
		}
	}
	$select .= '</select>';

	$sS = '<textarea id="q" rows="10" cols="100" onclick="BB.setArea(this.id)" name="ss" style="width:100%;height:300px;">'.Parser::getInstance()->unParseBB(stripslashes($nws['sstory'])).'</textarea>';
	$fS = '<textarea id="w" rows="10" cols="100" onclick="BB.setArea(this.id)" name="fs" style="width:100%;height:300px;">'.Parser::getInstance()->unParseBB(stripslashes($nws['fstory'])).'</textarea>';
	$subm = '<input type="submit" value="'.__('Зберегти').'"/>';

	Aurus::getInstance()->addContent( Aurus::getInstance()->form_start().Html::table(
	Html::tr(
	Html::td(__('Назва новини'),'','left','10%').
	Html::td('<input type="text" name="title" value="'.stripslashes($nws['title']).'" />')
	).
	Html::tr(
	Html::td(__('Категорія новини'),'','left','10%').
	Html::td($select)
	).
	Html::tr(
	Html::td('','','left','10%').
	Html::td(Aurus::getInstance()->getEditor())
	).
	Html::tr(
	Html::td(__('Короткий опис'),'','left','10%').
	Html::td($sS)
	).
	Html::tr(
	Html::td(__('Повна новина'),'','left','10%').
	Html::td($fS)
	)
	).$subm.'</form>');
}